package org.shan.hangtian.ui;

import io.netty.channel.Channel;
import org.shan.hangtian.server.session.SessionManager;
import org.shan.hangtian.tp.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MusicPanel extends JPanel {
    private RunMode currMode;

    private JLabel label;

    private BaseMusic modeMusic;

    private CenterPanel centerPanel;

    public MusicPanel(RunMode currMode, BaseMusic modeMusic, Color fontColor, CenterPanel centerPanel) {
        this.currMode = currMode;
        this.modeMusic = modeMusic;
        this.centerPanel = centerPanel;
        setBackground(null);
        setOpaque(false);
        setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));

        {
            label = new JLabel(modeMusic.getName());
            label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            label.setFont(new Font("黑体", Font.PLAIN, 18));
            label.setForeground(fontColor);

            add(label);
            addListeners();
        }
    }

    private void addListeners() {
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                Channel onlineChannel = SessionManager.getInstance().getOnlineChannel();
                if (onlineChannel == null) {
                    JOptionPane.showMessageDialog(MusicPanel.this, "未连接设备");
                    return;
                }

                if (Mode1Music.M1.equals(modeMusic))
                    centerPanel.changeMode(RunMode.MODE_1);
                else if (Mode2Music.M1.equals(modeMusic))
                    centerPanel.changeMode(RunMode.MODE_2);
                else {
                    onlineChannel.writeAndFlush(HangtianPacket.changeMusic(currMode, (byte) (modeMusic.getCode() - 1)));
                    centerPanel.changeMusic(modeMusic);
                }
            }
        });
    }
}
